package com.play.ballen.me.vm

import com.play.ballen.R
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil
import io.reactivex.Observable
import java.util.concurrent.TimeUnit


class  LoginVM: BaseUserViewModel() {

    val actionLoginSuccess = SingleLiveEvent<Void>()
    private val timeCountDown = 59L

    val actionCountDownComplete = SingleLiveEvent<Void>()
    val actionCountDown = SingleLiveEvent<Long>()
//    val userInfo = MutableLiveData<UserInfo>()//登录时候反回的信息
//    val UserData = MutableLiveData<UserData>()//查询用户基本信息



    fun sendCode(name: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["phone"] = name
        model.sendCode(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                startCountDown()
                Logger.i("RegisterVM.sendCode success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("RegisterVM.sendCode error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    private fun startCountDown() {
        Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(timeCountDown + 1)
            .map { timeCountDown - it }
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                actionCountDownComplete.call()
            }
            .subscribe({
                actionCountDown.value = it
            }, { throwable ->
                throwable.printStackTrace()
                Logger.i("RegisterVM.startCountDown error,msg = ${throwable.message}")
            }, {
            }).addToDisposable(compositeDisposable)
    }
    fun login(account: String, code: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading), false)
        val map = mutableMapOf<String, Any>()
        map["phone"] = account
        map["code"] = code
        model.login(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("LoginVM.login success")
                getUserInfo(it)
            }, { throwable ->
                tokenStatus.value=-1
                imLoginStatus.value=-1
                toastLiveData.value = throwable.message
                Logger.i("LoginVM.login error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun loginAli(token: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading), false)
        val map = mutableMapOf<String, Any>()
        map["token"] = token
        model.loginali(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("LoginVM.login success")
                getUserInfo(it)
            }, { throwable ->
                tokenStatus.value=-1
                imLoginStatus.value=-1
                toastLiveData.value = throwable.message
                Logger.i("LoginVM.login error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }





}